<?php

namespace App\Models\Factory;

use App\Constants\CreditLogConstant;
use App\Helpers\Logger\SLogger;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\AwUserAccountLog;
use App\Models\Orm\AwUserIntegral;
use App\Models\Orm\AwUserIntegralLog;
use App\Models\Orm\AwUserInvite;
use App\Models\Orm\User;
use App\Models\Orm\User_credit_type;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserCredit;
use App\Models\Orm\UserCreditLog;
use App\Models\Orm\UserCreditType;
use App\Models\Orm\UserCreditTypeUser;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserProfile;
use App\Strategies\CreditStrategy;


/**
 * 用户积分工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class UserCreditFactory extends AbsModelFactory
{

    //获取积分规则
    public static function getUserCredit($userId)
    {
       $userCreditType = UserCreditType::where('status',1)->get()->toArray();

    //生成一个邀请码
       $code = UserInviteCodeFactory::fetchInviteCode($userId);
        foreach ($userCreditType as $k => $v)
        {
            $userCreditTypeId = $v['id'];
            $userCreditType[$k]['status'] = self::getUserCreditType($userId,$userCreditTypeId)? '已完成':'+'.$v['code'];
            $userCreditType[$k]['url'] = "http://test.m.anwenqianbao.com/#/login".'?inviteCode='.$code;
            $userCreditType[$k]['code'] = '积分+'.$userCreditType[$k]['code'];
            $userCreditType[$k]['bg'] =  self::getUserCreditType($userId,$userCreditTypeId) ? 'FFAE2D' : 'cccccc';
            //如果完成就给这个用户加积分

            switch ($k)
            {
                case 0:
                 $userCreditType[$k]['img'] = 'http://qiniu.shoujijiekuan.com/image/1503571133.png';
                break;
                case 1:
                $userCreditType[$k]['img'] = 'http://qiniu.shoujijiekuan.com/image/1503571166.png';
                break;
                case 2:
                $userCreditType[$k]['img'] = 'http://qiniu.shoujijiekuan.com/image/1503571177.png';
                break;
                case 3:
                    $userCreditType[$k]['img'] = 'http://qiniu.shoujijiekuan.com/image/1503571191.png';
                    break;
                default:
                break;
            }

        }
        return $userCreditType ;
    }

    //拿到用户每个积分规则的完成状态
    public static function getUserCreditType($userId,$userCreditTypeId)
    {
        $result  = UserCreditTypeUser::where('user_id',$userId)->where('user_credit_type_id',$userCreditTypeId)->get()->toArray();

       return empty($result) ? 0 :1;

    }


    //用户总积分
    public static function getUserCreditTotal($userId){

        $userCredit = AwUserIntegral::where('user_id',$userId)->first();

        return $userCredit ? $userCredit->integral : 0;
    }


    //积分榜首
    public static  function getTopCredits()
    {
        $topCredits = AwUserIntegral::select('integral')->max('integral');
        return  $topCredits ? $topCredits : 0;

    }


    //兑换积分更新用户积分表
    public static function updateCredit($params)
    {
        $AwIntegralObj = AwUserIntegral::where('user_id',$params['userId'])->lockForUpdate()->first();

        $AwIntegralObj -> integral -=$params['credit'];
        $AwIntegralObj -> used += $params['credit'];

        return $AwIntegralObj ->save();
    }

    //邀请好友得积分
    public static  function updateCreditInvite($user_id,$integral)
    {
        $user_integal_obj = AwUserIntegral::where('user_id',$user_id)->first();
        $user_integal_obj->integral += $integral;
        return $user_integal_obj ->save();
    }



    //邀请好友来判断获得多少积分

    public static  function getIncome($userId)
    {
        //邀请好友数
        $inviteNum = UserInvite::where('user_id',$userId)->first()->invite_num;
        //通过规则判断用户拿多少分
        $codes = UserCreditType::where('status',1)->get()->toArray();

        switch ($inviteNum)
        {
            case 1:
                $income = $codes[0]['code'];
                break;
            case 2:
                $income = $codes[1]['code'];
                break;
            case 3:
                $income = $codes[2]['code'];
                break;
            default:
                $income = $codes[3]['code'];
                break;
        }
        return $income;
    }

    //插入积分日志表

    public static function createUserCreditLog($params)
    {
        $user = User::where('id',$params['userId'])->first();
        // 生成资金记录
        $insert_data = [
            'user_id' => $params['userId'],
            'action' => AwUserIntegralLog::ACTION_EXCHANGE,
            'order_id' => $params['order_id'],
            'type' => AwUserIntegralLog::TYPE_CUT,
            'status' => AwUserIntegralLog::STATUS_ACCESS,
            'integral' => $params['credit'],
            'user_integral' => $user->getIntegral()->integral
        ];

        $result = AwUserIntegralLog::insert($insert_data);

        return $result ? true :false;

    }

    //插入积分日志表
    public static  function createCreditLogInvite($invite_id,$user_id,$integral,$action,$type,$status,$userIntegral)
    {
        // 生成资金记录
        $insert_data = [
            'user_id' => $user_id,
            'action' => $action,
            'order_id' => $invite_id,
            'type' => $type,
            'status' => $status,
            'integral' => $integral,
            'user_integral' => $userIntegral
        ];

        $result = AwUserIntegralLog::insert($insert_data);

        return $result ? true : false;
    }


}